from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework.response import Response
from dvadmin.utils.viewset import CustomModelViewSet
from .models import Supplier, SupplierEvaluation, SupplierProduct
from .serializers import (
    SupplierSerializer, 
    SupplierCreateUpdateSerializer,
    SupplierEvaluationSerializer,
    SupplierEvaluationCreateUpdateSerializer,
    SupplierProductSerializer,
    SupplierProductCreateUpdateSerializer
)

# Create your views here.

class SupplierModelViewSet(CustomModelViewSet):
    """
    供应商管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Supplier.objects.all()
    serializer_class = SupplierSerializer
    create_serializer_class = SupplierCreateUpdateSerializer
    update_serializer_class = SupplierCreateUpdateSerializer
    filter_fields = ['name', 'code', 'status', 'credit_rating']
    search_fields = ['name', 'code', 'contact_person', 'contact_phone', 'address']

class SupplierEvaluationModelViewSet(CustomModelViewSet):
    """
    供应商评估接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = SupplierEvaluation.objects.all()
    serializer_class = SupplierEvaluationSerializer
    create_serializer_class = SupplierEvaluationCreateUpdateSerializer
    update_serializer_class = SupplierEvaluationCreateUpdateSerializer
    filter_fields = ['supplier', 'evaluation_date', 'evaluator']
    search_fields = ['evaluation_content', 'evaluator']

class SupplierViewSet(CustomModelViewSet):
    """供应商视图集"""
    queryset = Supplier.objects.all()
    serializer_class = SupplierSerializer
    create_serializer_class = SupplierCreateUpdateSerializer
    update_serializer_class = SupplierCreateUpdateSerializer
    
    # 配置导出字段
    export_field_label = {
        "name": "供应商名称",
        "code": "供应商编码",
        "contact_person": "联系人",
        "contact_phone": "联系电话",
        "email": "电子邮箱", 
        "address": "地址",
        "business_scope": "经营范围",
        "status": "状态",
        "credit_rating": "信用等级",
        "remarks": "备注"
    }
    
    @action(detail=True, methods=['get'])
    def products(self, request, pk=None):
        """获取供应商的产品列表"""
        supplier = self.get_object()
        products = SupplierProduct.objects.filter(supplier=supplier)
        serializer = SupplierProductSerializer(products, many=True)
        return Response(serializer.data)

class SupplierEvaluationViewSet(CustomModelViewSet):
    """供应商评估视图集"""
    queryset = SupplierEvaluation.objects.all()
    serializer_class = SupplierEvaluationSerializer
    create_serializer_class = SupplierEvaluationCreateUpdateSerializer
    update_serializer_class = SupplierEvaluationCreateUpdateSerializer
    
    # 配置导出字段
    export_field_label = {
        "supplier__name": "供应商名称",
        "evaluation_date": "评估日期",
        "quality_score": "质量评分",
        "delivery_score": "交付评分",
        "price_score": "价格评分",
        "service_score": "服务评分",
        "total_score": "总评分",
        "evaluator": "评估人",
        "evaluation_content": "评估内容"
    }
    
    # 根据供应商ID过滤记录
    def get_queryset(self):
        queryset = super().get_queryset()
        supplier_id = self.request.query_params.get('supplier_id', None)
        if supplier_id:
            queryset = queryset.filter(supplier_id=supplier_id)
        return queryset

class SupplierProductViewSet(CustomModelViewSet):
    """供应商产品视图集"""
    queryset = SupplierProduct.objects.all()
    serializer_class = SupplierProductSerializer
    create_serializer_class = SupplierProductCreateUpdateSerializer
    update_serializer_class = SupplierProductCreateUpdateSerializer
    
    # 配置导出字段
    export_field_label = {
        "supplier__name": "供应商名称",
        "product_name": "产品名称",
        "product_code": "产品编码",
        "category": "产品类别",
        "specification": "规格型号",
        "unit": "计量单位",
        "price": "价格",
        "min_order_quantity": "最小订购量",
        "lead_time": "供货周期(天)",
        "quality_standard": "质量标准",
        "is_active": "是否有效",
        "remarks": "备注"
    }
    
    # 根据供应商ID过滤记录
    def get_queryset(self):
        queryset = super().get_queryset()
        supplier_id = self.request.query_params.get('supplier_id', None)
        if supplier_id:
            queryset = queryset.filter(supplier_id=supplier_id)
        return queryset
